package com.higgins.lawapp.server;

import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.spreadsheet.*;
import com.higgins.lawapp.client.application.dataModel.LawFirm;
import java.io.IOException;
import java.net.*;
import java.util.*;

public class GoogleSpreadsheetService {
    private String USERNAME = "pshigginswork@gmail.com";
    private String PASSWORD = "Pa$$w0rd23";
	
	public SpreadsheetService openService()	{
		SpreadsheetService service  = null;
		try {
		    service = new SpreadsheetService("LawFirmSheet");
		    service.setUserCredentials(USERNAME, PASSWORD);
		    return service;
		} catch (Exception e)	{
			return null;
		}
	}
	
	
	public SpreadsheetEntry openSpreadSheet(SpreadsheetService service, String spreadsheetName)	{
	    SpreadsheetEntry mySpreadsheet = null;
	
	    try	{
		    URL SPREADSHEET_FEED_URL = new URL(
	        "https://spreadsheets.google.com/feeds/spreadsheets/private/full");

		    // Make a request to the API and get all spreadsheets.
		    SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
		    List<SpreadsheetEntry> spreadsheets = feed.getEntries();
	
		    // Iterate through all of the spreadsheets returned
		    for (SpreadsheetEntry spreadsheet : spreadsheets) {
		      // Print the title of this spreadsheet to the screen
		      //System.out.println(spreadsheet.getTitle().getPlainText());
		      if (spreadsheet.getTitle().getPlainText().equals(spreadsheetName))
		    	   return spreadsheet;
		    }
	    } catch (Exception e)	{
	    	
	    }
	    return null;
	}
	
	public ArrayList<ArrayList<String>>  getSpreadsheetData(SpreadsheetService service, SpreadsheetEntry entry )	{
		List<WorksheetEntry> worksheets = null;
		try	{	
			worksheets = entry.getWorksheets();
		    ArrayList<ArrayList<String>> spreadsheetData = new ArrayList<ArrayList<String>>();
	    	WorksheetEntry worksheet = worksheets.get(0);
	        // Get the worksheet's title, row count, and column count.
		    String title = worksheet.getTitle().getPlainText();
		    int rowCount = worksheet.getRowCount();
		    int colCount = worksheet.getColCount();

		    // Print the fetched information to the screen for this worksheet.
		    System.out.println("\t" + title + "- rows:" + rowCount + " cols: " + colCount);
		      

		    URL listFeedUrl = worksheet.getListFeedUrl();
		    ListFeed listFeed = service.getFeed(listFeedUrl, ListFeed.class);

		    // Iterate through each row, printing its cell values.
		    int rowPointer = 0;
		    for (ListEntry row : listFeed.getEntries()) {
		    	String lawFirm = null;
		    	String address = null;
		    	String contact = null;
		    	int colPointer = 0;
		    	for (String tag : row.getCustomElements().getTags()) {
		    		if (colPointer==0)	
		    			lawFirm = row.getCustomElements().getValue(tag);
		    		if (colPointer==1)	
		    			address = row.getCustomElements().getValue(tag);
		    		if (colPointer==2)	
		    			contact = row.getCustomElements().getValue(tag);
		    		colPointer++;
		    	}
		    	System.out.println();
		    	ArrayList<String> list = new ArrayList<String>();
		    	list.add(lawFirm);
		    	list.add(address);
		    	list.add(contact);
		    	spreadsheetData.add(list);
		    	rowPointer++;
		    }	
		    return spreadsheetData;
  		}	catch (Exception e)	{
	    }		
  		return null;
	}
	
	public void setSpreadsheetData(SpreadsheetService service, SpreadsheetEntry entry, 
			List<LawFirm> lawFirms)	{
		List<WorksheetEntry> worksheets = null;
		try	{	
			int row = 0;
			worksheets = entry.getWorksheets();
	    	WorksheetEntry worksheet = worksheets.get(0);
		    URL cellFeedUrl = worksheet.getCellFeedUrl();
		    CellFeed listFeed = service.getFeed(cellFeedUrl, CellFeed.class);
		    for (CellEntry cell : listFeed.getEntries()) {
		    	if (cell.getTitle().getPlainText().equals("A1"))
		    		writeFirstTitle(cell);
		    	else if (cell.getTitle().getPlainText().equals("B1"))
		    		writeSecondTitle(cell);
		    	else if (cell.getTitle().getPlainText().equals("C1"))
		    		writeThirdTitle(cell);
		    	else if (cell.getTitle().getPlainText().substring(0, 1).equals("A"))
		    		writeFirstCell(cell, lawFirms.size() > row ?lawFirms.get(row).getLawFirm():"");
		    	else if (cell.getTitle().getPlainText().substring(0, 1).equals("B"))
		    		writeSecondCell(cell, lawFirms.size() > row ?lawFirms.get(row).getAddress():"");
		    	else if (cell.getTitle().getPlainText().substring(0, 1).equals("C"))
		    		writeThirdCell(cell, lawFirms.size() > row ?lawFirms.get(row++).getContact():"");
	    	}
  		} catch (Exception e) {
  			System.out.println("Exception " + e.toString());
	    } catch (Throwable e) {
  			System.out.println("Exception " + e.toString());
		}		
	}	
	private void writeFirstTitle(CellEntry cell) throws IOException, Throwable	{
    	cell.changeInputValueLocal("Law Firm");
    	cell.update();
	}
	private void writeSecondTitle(CellEntry cell) throws IOException, Throwable	{
    	cell.changeInputValueLocal("Address");
    	cell.update();
	}
	private void writeThirdTitle(CellEntry cell) throws IOException, Throwable	{
    	cell.changeInputValueLocal("Contact");
    	cell.update();
	}
	
	private void writeFirstCell(CellEntry cell, String lawFirm) throws IOException, Throwable	{
    	cell.changeInputValueLocal(lawFirm);
    	cell.update();
	}
	private void writeSecondCell(CellEntry cell, String address) throws IOException, Throwable	{
    	cell.changeInputValueLocal(address);
    	cell.update();
	}
	private void writeThirdCell(CellEntry cell, String contact) throws IOException, Throwable	{
    	cell.changeInputValueLocal(contact);
    	cell.update();
	}
	
}
